<template>
  <div class="app-container">
    <!--    搜索-->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="优惠券状态" prop="couponName">
        <template>
          <el-select v-model="queryParams.status" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </template>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['coupon:coupon:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['coupon:coupon:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['coupon:coupon:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['coupon:coupon:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <!--列表-->
    <el-table v-loading="loading" :data="couponList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="优惠券id" align="center" prop="couponId" />
      <el-table-column label="优惠券名称" align="center" prop="couponName" />
      <el-table-column label="发布数量" align="center" prop="couponQuantity" />
      <el-table-column label="适用商品" align="center" prop="applyProduct">
        <template slot-scope="scope">
          <span style="margin-left: 10px" v-if="scope.row.applyProduct == 1">通用</span>
          <span style="margin-left: 10px" v-if="scope.row.applyProduct == 2">指定商品</span>
          <span style="margin-left: 10px" v-if="scope.row.applyProduct == 3">指定分类</span>
        </template>
      </el-table-column>
      <el-table-column label="使用开始时间" align="center" prop="useStartTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.useStartTime, '{y}-{m}-{d}') }}~</span>
          <span>{{ parseTime(scope.row.useEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="使用门槛" align="center" prop="employDoorsill" />
      <el-table-column label="优惠券类型" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{scope.row.name}}</span>
        </template>
      </el-table-column>
      <el-table-column label="每人限制次数" align="center" prop="astrictFrequency" />
      <el-table-column label="获取方式" align="center" prop="acquireMode" />
      <el-table-column label="优惠劵状态" align="center" prop="status" >
        <template slot-scope="scope">
          <span style="margin-left: 10px" v-if="scope.row.status == 1">已审核</span>
          <span style="margin-left: 10px" v-if="scope.row.status == 2">失效</span>
          <span style="margin-left: 10px" v-if="scope.row.status == 3">未审核</span>
          <span style="margin-left: 10px" v-if="scope.row.status == 4">审核不通过</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >不同意</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="loseEfficacy(scope.row)"
          >同意</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--  分页-->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改优惠卷对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form1" :rules="rules" label-width="80px">
        <el-form-item label="优惠券名称" prop="couponName">
          <el-input v-model="form1.couponName" placeholder="请输入优惠券名称" v-if="title == '添加优惠卷'"/>
          <el-input v-model="form1.couponName" placeholder="请输入优惠券名称" v-if="title == '修改优惠卷'" readonly/>
        </el-form-item>
        <el-form-item label="发布数量" prop="couponQuantity">
          <el-input v-model="form1.couponQuantity" placeholder="请输入发布数量" v-if="title == '添加优惠卷'"/>
          <el-input v-model="form1.couponQuantity" placeholder="请输入发布数量" v-if="title == '修改优惠卷'" readonly/>
        </el-form-item>
        <el-form-item label="适用商品" prop="applyProduct">
          <template v-if="title == '添加优惠卷'">
            <el-radio v-model="form1.applyProduct"  :label="1">通用</el-radio>
            <el-radio v-model="form1.applyProduct"  :label="2">指定商品</el-radio>
          </template>
          <template v-if="title == '修改优惠卷'" readonly>
            <el-radio v-model="form1.applyProduct" :label="1">通用</el-radio>
            <el-radio v-model="form1.applyProduct" :label="2">指定商品</el-radio>
          </template>
        </el-form-item>
        <el-form-item label="指定商品" prop="skuIds" v-if="form1.applyProduct == 2">
          <el-input v-model="form1.skuIds" />
        </el-form-item>
        <template>
          <el-table :data="tableData" border style="width: 100%" v-if="form1.applyProduct == 2">
            <el-table-column fixed prop="skuId" label="skuId" width="50">
              <template slot-scope="scope">
                <span style="margin-left: 10px">{{scope.row.skuId}}</span>
              </template>
            </el-table-column>
            <el-table-column fixed prop="skuName" label="sku名称" width="150"></el-table-column>
            <el-table-column fixed prop="price" label="价格" width="50"></el-table-column>
            <el-table-column fixed prop="inventory" label="库存" width="50"></el-table-column>
            <el-table-column fixed="right" label="操作" width="100">
              <template slot-scope="scope">
                <el-button @click="handleClick(scope.row)" type="text" size="small">选择</el-button>
              </template>
            </el-table-column>
          </el-table>
        </template>
        <el-form-item label="使用开始时间" prop="useStartTime">
          <el-date-picker clearable
                          v-model="form1.useStartTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择使用开始时间" v-if="title == '添加优惠卷'">
          </el-date-picker>
          <el-date-picker clearable
                          v-model="form1.useStartTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择使用开始时间" v-if="title == '修改优惠卷'" readonly>
          </el-date-picker>
        </el-form-item>
        <el-form-item label="使用结束时间" prop="useEndTime">
          <el-date-picker clearable
                          v-model="form1.useEndTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择使用结束时间" v-if="title == '添加优惠卷'">
          </el-date-picker>
          <el-date-picker clearable
                          v-model="form1.useEndTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择使用结束时间" v-if="title == '修改优惠卷'" readonly>
          </el-date-picker>
        </el-form-item>
        <el-form-item label="使用门槛" prop="employDoorsill">
          <el-input v-model="form1.employDoorsill" placeholder="请输入使用门槛" v-if="title == '添加优惠卷'"/>
          <el-input v-model="form1.employDoorsill" placeholder="请输入使用门槛" v-if="title == '修改优惠卷'" readonly/>
        </el-form-item>
        <el-form-item label="领取开始时间" prop="drawStartTime" v-if="title == '修改优惠卷'">
          <el-date-picker clearable
                          v-model="form1.drawStartTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择领取开始时间" v-if="title == '添加优惠卷'">
          </el-date-picker>
          <el-date-picker clearable
                          v-model="form1.drawStartTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择领取开始时间" v-if="title == '修改优惠卷'" readonly>
          </el-date-picker>
        </el-form-item>
        <el-form-item label="领取结束时间" prop="drawEndTime" v-if="title == '修改优惠卷'">
          <el-date-picker clearable
                          v-model="form1.drawEndTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择领取结束时间" v-if="title == '添加优惠卷'">
          </el-date-picker>
          <el-date-picker clearable
                          v-model="form1.drawEndTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择领取结束时间" v-if="title == '修改优惠卷'" readonly>
          </el-date-picker>
        </el-form-item>
        <el-form-item label="每人限制次数" prop="astrictFrequency">
          <el-input v-model="form1.astrictFrequency" placeholder="请输入每人限制次数" v-if="title == '添加优惠卷'"/>
          <el-input v-model="form1.astrictFrequency" placeholder="请输入每人限制次数" v-if="title == '修改优惠卷'" readonly/>
        </el-form-item>
        <el-form-item label="获取方式" prop="acquireMode">
          <el-input v-model="form1.acquireMode" placeholder="请输入获取方式(免费赠送写死)" v-if="title == '添加优惠卷'"/>
          <el-input v-model="form1.acquireMode" placeholder="请输入获取方式(免费赠送写死)" v-if="title == '修改优惠卷'" readonly/>
        </el-form-item>
        <el-form-item label="优惠券类型" prop="applyProduct" v-if="title == '添加优惠卷'">
          <template>
            <el-radio v-model="form1.couponId"  :label="1">满减券</el-radio>
            <el-radio v-model="form1.couponId"  :label="2">折扣券</el-radio>
            <el-radio v-model="form1.couponId"  :label="3">随机券</el-radio>
          </template>
        </el-form-item>
        <el-form-item label="满减" prop="astrictFrequency" v-if="form1.couponId == 1 && title == '添加优惠卷'" >
          <el-input v-model="form1.value1" v-if="title == '添加优惠卷'"/>
        </el-form-item>
        <el-form-item label="折扣" prop="astrictFrequency" v-if="form1.couponId == 2 && title == '添加优惠卷'">
          <el-input v-model="form1.value1" v-if="title == '添加优惠卷'"/>
        </el-form-item>
        <el-form-item label="最小金额" prop="astrictFrequency" v-if="form1.couponId == 3 && title == '添加优惠卷'">
          <el-input v-model="form1.value1" v-if="title == '添加优惠卷'"/>
        </el-form-item>
        <el-form-item label="最大金额" prop="astrictFrequency" v-if="form1.couponId == 3 && title == '添加优惠卷'">
          <el-input v-model="form1.value2" v-if="title == '添加优惠卷'"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="title == '添加优惠卷'">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>


import { delCoupon, getCoupon, listCoupon, updateCoupon, updateCoupon2 } from '@/api/platform/platform'
import { addCoupon, publishAllItemsInCoupon } from '@/api/coupon/coupon'

export default {
  name: "Coupon",
  data() {
    return {
      // 审核参数
      audit: {
        id: 0 ,
        status: 0
      },
      // 静态下拉框
      options: [{
        value: '1',
        label: '已审核'
      }, {
        value: '3',
        label: '未审核'
      }, {
        value: '2',
        label: '失效'
      }, {
        value: '4',
        label: '审核不通过'
      }],
      // 列表
      tableData: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 优惠卷表格数据
      couponList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        couponName: null,
        couponQuantity: null,
        applyProduct: null,
        useStartTime: null,
        useEndTime: null,
        employDoorsill: null,
        drawStartTime: null,
        drawEndTime: null,
        couponType: null,
        astrictFrequency: null,

        acquireMode: null
      },
      // 表单参数
      form1: {
        skuIds: [],
        // 优惠券名称
        couponName: null,
        // 发布数量
        couponQuantity: null,
        // 适用商品
        applyProduct: null,
        // 使用开始时间
        useStartTime: null,
        // 使用结束时间
        useEndTime: null,
        // 使用门槛
        employDoorsill: null,
        // 领取开始时间
        drawStartTime: null,
        // 领取结束时间
        drawEndTime: null,
        // 每人限制次数
        astrictFrequency: null,
        // 获取方式
        acquireMode: '赠送',
        value1: null,
        value2: null
      },
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 多条件查询优惠卷分页列表 */
    getList() {
      this.loading = true;
      listCoupon(this.queryParams).then(response => {
        console.log('多条件查询优惠卷分页列表',response)
        this.couponList = response.data.rows;
        this.total = response.data.total;
        console.log('total',this.total)
        this.loading = false;
      });
      var id = 1
      publishAllItemsInCoupon(id).then(res => {
        console.log('B端 发布优惠劵页面 通过商铺id查询所有商品',res)
        this.tableData = res.data
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        couponId: null,
        couponName: null,
        couponQuantity: null,
        applyProduct: null,
        useStartTime: null,
        useEndTime: null,
        employDoorsill: null,
        drawStartTime: null,
        drawEndTime: null,
        couponType: null,
        astrictFrequency: null,
        status: null,
        createTime: null,
        acquireMode: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.couponId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加优惠卷";
    },
    /** 查询按钮操作 */
    handleUpdate(row) {
      this.reset();
      const couponId = row.couponId || this.ids
      getCoupon(couponId).then(response => {
        console.log("查看:",response)
        this.form1 = response.data;
        this.form1.skuIds = response.data.shoparray;
        this.open = true;
        this.title = "修改优惠卷";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.couponId != null) {
            updateCoupon(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            console.log("添加参数:",this.form1)
            addCoupon(this.form1).then(response => {
              console.log("添加返回值:",response)
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 不同意按钮操作 */
    handleDelete(row) {
      const couponIds = row.couponId || this.ids;
      this.$modal.confirm('是否确认不同意优惠卷编号为"' + couponIds + '"的数据项？').then(function() {
        return delCoupon(couponIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("不同意成功");
      }).catch(() => {
      });
    },
    /** 同意按钮操作 */
    loseEfficacy(row) {
      const couponIds = row.couponId || this.ids;
      this.$modal.confirm('是否确认同意优惠卷编号为"' + couponIds + '"的数据项？').then(function() {
        return updateCoupon2(couponIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("同意成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('coupon/coupon/export', {
        ...this.queryParams
      }, `coupon_${new Date().getTime()}.xlsx`)
    },
    /** 选中的商品 **/
    handleClick(row) {
      console.log("选中的商品skuId", row.skuId)
      this.form1.skuIds.push(row.skuId)
      console.log(this.form1.skuIds)
    }
  }
};
</script>
